/**
 * 
 */
package org.gov.vis.master.gui.listener;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.ResourceBundle;

import javax.swing.JOptionPane;

import org.gov.vis.common.exceptions.InvalidInputException;
import org.gov.vis.gui.util.ErrorsDisplayJPanel;
import org.gov.vis.master.controller.OtherCtlr;
import org.gov.vis.master.controller.OtherCtlrImpl;
import org.gov.vis.master.dto.OtherDto;
import org.gov.vis.master.gui.OthersDialog;
import org.gov.vis.util.GuiConstants;
import org.gov.vis.util.Util;


/**
 * @author Vishwanath
 *
 */
public class AddOtherListener implements ActionListener,BaseListener {

	private OthersDialog othersDailog;
	private OtherCtlr otherCtlr = new OtherCtlrImpl();

	//private ErrorsDisplayJPanel errorMsgPanel;
	final private ResourceBundle rb = Util.getResources();
	private OtherDto otherDto;
	private ErrorsDisplayJPanel errorMsgPanel;

	/* (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */

	public AddOtherListener(OthersDialog othersDialog){

		this.othersDailog = othersDialog;
	}
	@Override
	public void actionPerformed(ActionEvent actionEvent) {
		try{
			errorMsgPanel = new ErrorsDisplayJPanel(othersDailog.getjPanel1());
			errorMsgPanel.clearMsgPanel();
			
			if(actionEvent.getActionCommand().equals(rb.getString("dialog_button_save"))
					|| actionEvent.getActionCommand().equals(rb.getString("dialog_button_update"))){
				//Method called to either Add or Edit the Other details
				addOrEditOther();
			}else if(actionEvent.getActionCommand().equals(rb.getString("dialog_button_view"))){	
				//Method called to view the details of Other Object based on the passed otherId
				viewOther(1);
			}else if(actionEvent.getActionCommand().equals(rb.getString("dialog_button_delete"))){
				//Method called to delete the Other Object based on the passed otherId.
				deleteOther(1);
			}else if(actionEvent.getActionCommand().equals(rb.getString("dialog_button_reset"))){
				//Method called to clear the screen data
				clear();
			}
		}catch(Exception e){
			e.printStackTrace();
		}

	}//end actionPerformed


	/**
	 * Method called to delete the Other Object based on the passed otherId
	 * @param _otherId
	 */
	private void deleteOther(final long  _otherId){
		try{
			otherCtlr.deleteOther(_otherId);
		}catch(Exception e){
			e.getMessage();
		}
	}//end deleteOther
	
	/**
	 * Method called to either Add or Edit the Other details
	 */
	private void addOrEditOther(){
		//Method called to set the Field's Labels to default Black color
		clearGUILabelColors(blackColor);

		//Method to set the Screen's data to  the DTO Object
		setGuiDetailsToDto(othersDailog);

		try{
			//Method called to Add or Edit the Other
			/*otherCtlr.addOrEditOther(otherDto);
			 List<String> successMsgLst = new ArrayList<String>();
			 successMsgLst.add(rb.getString(GuiConstants.MASTER_ADD_OTHER_SUCCESS));
			 errorMsgPanel.setSuccessMessages(successMsgLst);
			*///othersDailog.getLblResultSuccess().setText("Other Details added successfully");
			//othersDailog.getLblResultSuccess().setForeground(Color.GREEN);
			//othersDailog.validate();
			//othersDailog.repaint();
			 otherCtlr.addOrEditOther(otherDto);
				JOptionPane.showMessageDialog(othersDailog, rb.getString(GuiConstants.MASTER_ADD_OTHER_SUCCESS));
				//othersDailog.dispose();	


			//Method called to clear the screen data
			clear();
		}catch(Exception e){
			//e.printStackTrace();
			 List<String> errorMsgLst = new ArrayList<String>();
			if(e instanceof InvalidInputException){
				final HashMap<String,String> _exceptions = OtherCtlrImpl.errorMsgs;
				if (_exceptions != null && !_exceptions.isEmpty()) {
					String errMsg = null;
					if(_exceptions.containsKey(rb.getString("dialog_label_othr_code").toUpperCase())){
						othersDailog.getLblCode().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_othr_code").toUpperCase());
						errorMsgLst.add(errMsg);
					}

					if(_exceptions.containsKey(rb.getString("dialog_label_othr_nmeoforg").toUpperCase())){
						othersDailog.getLblOrganizationName().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_othr_nmeoforg").toUpperCase());
						errorMsgLst.add(errMsg);
					}
					
					errorMsgPanel.setErrorMessages(errorMsgLst);					
				} else {
					//log.debug(e.getMessage(), e);
				}
			}
			else{
				errorMsgLst.add(e.getMessage());
				errorMsgPanel.setErrorMessages(errorMsgLst);
			}
		}		
	}//end addOrEditOther

	/**
	 * Method called to view the Other details.
	 */
	private void viewOther(final long _otherId) {
		// TODO Auto-generated method stub
		try{
			final OtherDto _otherDto = otherCtlr.getOther(_otherId);
			setDtoDetailsToGui(_otherDto);
			othersDailog.validate();
			othersDailog.repaint();
		}catch(Exception e){
			e.printStackTrace();
		}

	}//end viewOther
	/**
	 * Method called to Clear all the form fields of the screen
	 */
	@Override
	public void clear() {
		// TODO Auto-generated method stub

		//set the Field's data to empty values
		othersDailog.getTxtFldCode().setText("");
		othersDailog.getTxtFldAIWorker().setText("");
		othersDailog.getTxtFldDistrict().setText("");
		othersDailog.getTxtFldHeadQuarter().setText("");
		othersDailog.getTxtFldJurisdiction().setText("");
		othersDailog.getTxtFldMandal().setText("");
		othersDailog.getTxtFldOrg().setText("");

		//Method called to set the Field's Labels to default Black color
		clearGUILabelColors(blackColor);

	}//end clear
	
	/**
	 * Method called to set the Field's Labels to color based on the passed Color object 
	 */
	@Override
	public void clearGUILabelColors(Color _lblColor){
		//set the Field's Labels to default Black color
		othersDailog.getLblCode().setForeground(_lblColor);
		othersDailog.getLblNameOfAIWorker().setForeground(_lblColor);
		othersDailog.getLblDistrict().setForeground(_lblColor);
		othersDailog.getLblHeadquarter().setForeground(_lblColor);
		othersDailog.getLblJurisdiction().setForeground(_lblColor);
		othersDailog.getLblMandal().setForeground(_lblColor);
		othersDailog.getLblOrganizationName().setForeground(_lblColor);

	}//end clearGUILabelColors

	/**
	 * Method called to read the data from GUI and set them to DTO Object 
	 * @param _guiObject
	 */
	@Override
	public void setDtoDetailsToGui(Object object) {
		// TODO Auto-generated method stub
		OtherDto _otherDto = (OtherDto)object;
		othersDailog.getTxtFldCode().setText(_otherDto.getCode());
		othersDailog.getTxtFldAIWorker().setText(_otherDto.getNameOfAIWorker());
		othersDailog.getTxtFldDistrict().setText(_otherDto.getDistrict());
		othersDailog.getTxtFldHeadQuarter().setText(_otherDto.getHeadQuarter());
		othersDailog.getTxtFldJurisdiction().setText(_otherDto.getJuridiction());
		othersDailog.getTxtFldMandal().setText(_otherDto.getMandal());
		othersDailog.getTxtFldOrg().setText(_otherDto.getNameOfOrg());

	}//end setDtoDetailsToGui

	/**
	 * Method called to read the data from DTO and set them to GUI.
	 * @param _guiObject
	 */
	@Override
	public void setGuiDetailsToDto(Object object) {
		// TODO Auto-generated method stub
		OthersDialog _othersDialog =(OthersDialog)object;
		otherDto = new OtherDto();
		otherDto.setCode(_othersDialog.getTxtFldCode().getText());
		otherDto.setNameOfOrg(_othersDialog.getTxtFldOrg().getText());
		otherDto.setNameOfAIWorker(_othersDialog.getTxtFldAIWorker().getText());
		otherDto.setHeadQuarter(_othersDialog.getTxtFldHeadQuarter().getText());
		otherDto.setJuridiction(_othersDialog.getTxtFldJurisdiction().getText());
		otherDto.setMandal(_othersDialog.getTxtFldMandal().getText());
		otherDto.setDistrict(_othersDialog.getTxtFldDistrict().getText());

	}//end setGuiDetailsToDto

	public void setOthersDailog(OthersDialog othersDailog) {
		this.othersDailog = othersDailog;
	}

	public OthersDialog getOthersDailog() {
		return othersDailog;
	}

	public void setOtherCtlr(OtherCtlr otherCtlr) {
		this.otherCtlr = otherCtlr;
	}

	public OtherCtlr getOtherCtlr() {
		return otherCtlr;
	}

}//end class
